//
//  MainTabBarController.m
//  weibo
//
//  Created by hp on 16/4/22.
//  Copyright © 2016年 hp. All rights reserved.
//

#import "MainTabBarController.h"
#import "HomeViewController.h"
#import "DiscoverViewController.h"
#import "MessageViewController.h"
#import "MeViewController.h"
@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBar.tintColor = [UIColor orangeColor];
    
    // 添加子视图控制器
    [self addChildViewControllers];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 添加中间按钮(放在viewDidLoad中的话，中间按钮不起作用，所以在最后添加)
    [self addComposeBtn];
}



- (void)addChildViewControllers
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MainVCSettings" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    for (NSDictionary *dic in arr) {
        
        [self addChildViewController:dic[@"vcName"] imageName:dic[@"imageName"] selectedImage:dic[@"selectedImageName"] title:dic[@"title"]];
    }
}


// 封装一下
- (void)addChildViewController:(NSString *)childControllerName imageName:(NSString *)imageName selectedImage:(NSString *)selectedImageName title:(NSString *)title
{
    // 字符串转换成类名
    UIViewController *vc = [[NSClassFromString(childControllerName) alloc] init];
    // tabbar上的图标
    
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    // tabbar 上面的文字
//    vc.tabBarItem.title = title;
    // 2、把首页嵌入到导航栏控制器
    UINavigationController *homeNavi = [[UINavigationController alloc] initWithRootViewController:vc];
//    vc.navigationItem.title = title;
    vc.title = title;
    [self addChildViewController:homeNavi];
}

- (void)addComposeBtn
{
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    int width  =[UIScreen mainScreen].bounds.size.width / 5.0f;
    btn.frame = CGRectMake(width*2, 0, width, 49);
    [btn addTarget:self action:@selector(composeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.tabBar addSubview:btn];
}
- (void)composeBtnPressed
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
